
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;


using Swaf;
using Swaf.BizObj;

namespace Swaf.BizObj.fieldValidator
{
	/// <summary> The concrete FieldValidator that optionally uses
	/// an arbitrary FieldValidator (the Decorator Design Pattern).
	/// </summary>
	[Serializable]
	public class OptionalValidator:FieldValidator
	{
		public static void register(IApplication app)
		{
			Swaf.BizObj.BizObjManager bizObjMgr = (Swaf.BizObj.BizObjManager)app.bizObjMgr;
			string pattern = "optional{optional-ws}({peek ;}|{peek-endstring})";
			string err = bizObjMgr.registerPrototype(pattern, new OptionalValidator("optional", null));
			if (err != null)
				Application.currentApp.Log.println(Application.NORMAL_STARTUP_LOG_LEVEL,"Unexpected problem during OptionalValidator static init: " + err);
		}
		
		public override string validateNoThrow(IBizObj bizObj, string name, string newValue)
		{
			if (newValue.Length == 0)
				return null;
			return inner_.validateNoThrow(bizObj, name, newValue);
		}
		
		/// <summary>This should never be called.
		/// </summary>
		protected internal override FieldValidator create(string context, string a, string b, string c)
		{
			parseError("Unexpected use of OptionalValidator.create(): " + context);
			return null;
		}
		
		/// <summary>Constructs an OptionalValidator using the specified FieldValidator, 'inner'.
		/// </summary>
		/// <param name="s">the String that was parsed to produce <c>inner</c>.
		/// </param>
		/// <param name="inner">the FieldValidator that resulted from parsing <c>s</c>.
		/// 
		/// </param>
		public OptionalValidator(string s, FieldValidator inner):base(s)
		{
			inner_ = inner;
		}
		
		/// <summary>This is the Validator that is Optional.
		/// </summary>
		protected internal FieldValidator inner_;
		
		public override string details(string prefix)
		{
			return prefix + "OptionalValidator(\"" + this + "\") {\n" + inner_.details("\t" + prefix) + "\n" + prefix + "}";
		}
		
	}
}